<?php
namespace order\impl;
use order\Order;
use tool\Price;

class Fx extends Order {

    public function make_pre($pre_order)
    {
        $price=new Price();
        $goods_list=$pre_order['goods_list'];
        foreach ($goods_list as $_goods){

            $price->changeprice($_goods['price'],"购买{$_goods['name']} 数量:{$_goods['buynumber']}",$_goods['buynumber']);
        }
        $pre_order['price_list']=$price->getprice_list();
        $pre_order['totalprice']=$price->gettotalprice();
        $params=$pre_order['params'];

        $fx_user_id=$params['fx_user_id'];
        $pre_order['extend']=["fx_user_id"=>$fx_user_id];

        return $pre_order;
    }

    public function make($order)
    {
        $price=new Price($order['price_list']);
        $order['price_list']=$price->getprice_list();
        $order['totalprice']=$price->gettotalprice();
        return $order;
    }

    public function notify($order)
    {
        $orderno=$order['orderno'];
        $fx_config=c("fx");
        $level1=intval($fx_config['level1']);
        $order_goods_list=model("orderGoodsList")->where("orderno",$orderno)->select();
        $order_goods=$order_goods_list[0];
        $goods_id=$order_goods['goods_id'];
        $fx_goods=model("fxGoods")->where("goods_id",$goods_id)->find();
        $cashback=$fx_goods['cashback'];
        $level2=intval($fx_config['level2']);

        $extend=json_decode($order['extend'],true);
        $fx_user_id=$extend['fx_user_id'];
        $fx_user_level1=model("fx_user")->where("user_id",$fx_user_id)->find();
        if(!empty($fx_user_level1)){
            fxmoney($fx_user_level1['user_id'],$cashback*$level1/100,"订单:{$orderno}一级佣金奖励");
        }

        $fx_user_level2=model("fx_user")->where("user_id",$fx_user_level1['up_user_id'])->find();
        if(!empty($fx_user_level2)){
            fxmoney($fx_user_level2['user_id'],$cashback*$level2/100,"订单:{$orderno}二级佣金奖励");
        }
        model("order")->where("orderno",$orderno)->update(["status"=>1,"ispay"=>1]);
        return "";
    }
}